-
Notifications
You must be signed in to change notification settings - Fork 15.4k
ClangFormat: Insert braces & newline for Chromium #140888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-format Author: Victor Vianna (victorvianna) ChangesThis addresses an old TODO in the Chromium codebase to upstream a change to the Chromium format style. Bug: crbug.com/40247920 Full diff: https://github.com/llvm/llvm-project/pull/140888.diff 5 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0cfa061681053..389a5f0ea0b5b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
ChromiumStyle.IncludeStyle.IncludeBlocks =
tooling::IncludeStyle::IBS_Preserve;
+ ChromiumStyle.InsertBraces = true;
+ ChromiumStyle.InsertNewlineAtEOF = true;
+
if (Language == FormatStyle::LK_Java) {
ChromiumStyle.AllowShortIfStatementsOnASingleLine =
FormatStyle::SIS_WithoutElse;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c0633ba3c29b3..e0664f46e1d72 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5420,7 +5420,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
"#endif\n"
"void bar() {\n"
" int y = 0;\n"
- "}",
+ "}\n",
style);
style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
@@ -5432,7 +5432,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
"#endif\n"
"void bar() {\n"
" int y = 0;\n"
- "}",
+ "}\n",
style);
style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
@@ -5444,7 +5444,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
"#endif\n"
"void bar() {\n"
" int y = 0;\n"
- "}",
+ "}\n",
style);
verifyFormat("#if 1\n"
" // some comments\n"
@@ -5454,7 +5454,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
"void bar() {\n"
" // comment\n"
" int y = 0;\n"
- "}",
+ "}\n",
"#if 1\n"
"// some comments\n"
"// another\n"
@@ -5463,7 +5463,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
"void bar() {\n"
" // comment\n"
" int y = 0;\n"
- "}",
+ "}\n",
style);
style.IndentPPDirectives = FormatStyle::PPDIS_None;
@@ -5473,7 +5473,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("if (emacs) {\n"
"#ifdef is\n"
@@ -5482,7 +5482,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" return duh(); \\\n"
" }\n"
"#endif\n"
- "}",
+ "}\n",
style);
verifyFormat("#if abc\n"
"#ifdef foo\n"
@@ -5494,7 +5494,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" } \\\n"
" D();\n"
"#endif\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#ifndef foo\n"
"#define foo\n"
@@ -5506,7 +5506,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" }\n"
"#endif\n"
"}\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#if 1\n"
"#define X \\\n"
@@ -5514,13 +5514,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" x; \\\n"
" x; \\\n"
" }\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#define X \\\n"
" { \\\n"
" x; \\\n"
" x; \\\n"
- " }",
+ " }\n",
style);
style.PPIndentWidth = 2;
@@ -5530,7 +5530,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
style.IndentWidth = 8;
verifyFormat("#ifdef foo\n"
@@ -5539,7 +5539,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
style.IndentWidth = 1;
@@ -5550,13 +5550,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" x; \\\n"
" x; \\\n"
" }\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#define X \\\n"
" { \\\n"
" x; \\\n"
" x; \\\n"
- " }",
+ " }\n",
style);
style.IndentWidth = 4;
@@ -5568,7 +5568,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#if abc\n"
"# ifdef foo\n"
@@ -5580,7 +5580,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" } \\\n"
" D();\n"
"# endif\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#ifndef foo\n"
"#define foo\n"
@@ -5592,13 +5592,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" }\n"
"#endif\n"
"}\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#define X \\\n"
" { \\\n"
" x; \\\n"
" x; \\\n"
- " }",
+ " }\n",
style);
style.PPIndentWidth = 2;
@@ -5609,7 +5609,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
style.PPIndentWidth = 4;
@@ -5618,7 +5618,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" { \\\n"
" x; \\\n"
" x; \\\n"
- " }",
+ " }\n",
style);
style.IndentWidth = 4;
@@ -5631,7 +5631,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" return duh(); \\\n"
" }\n"
"#endif\n"
- "}",
+ "}\n",
style);
verifyFormat("#if abc\n"
" #ifdef foo\n"
@@ -5641,7 +5641,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" } \\\n"
" C();\n"
" #endif\n"
- "#endif",
+ "#endif\n",
style);
verifyFormat("#if 1\n"
" #define X \\\n"
@@ -5649,7 +5649,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" x; \\\n"
" x; \\\n"
" }\n"
- "#endif",
+ "#endif\n",
style);
style.PPIndentWidth = 2;
@@ -5659,7 +5659,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" B(); \\\n"
" } \\\n"
" C();\n"
- "#endif",
+ "#endif\n",
style);
style.PPIndentWidth = 4;
@@ -5670,7 +5670,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
" x; \\\n"
" x; \\\n"
" }\n"
- "#endif",
+ "#endif\n",
style);
}
@@ -5904,7 +5904,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
getLLVMStyleWithColumns(60));
verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
- "(n, buffers))",
+ "(n, buffers))\n",
getChromiumStyle(FormatStyle::LK_Cpp));
// See PR41483
@@ -11605,7 +11605,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
getGoogleStyle());
- verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
+ verifyFormat("A<A<int>> a;\n", getChromiumStyle(FormatStyle::LK_Cpp));
// template closer followed by a token that starts with > or =
verifyFormat("bool b = a<1> > 1;");
@@ -12817,23 +12817,23 @@ TEST_F(FormatTest, AttributeClass) {
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
verifyFormat("class S {\n"
" S(S&&) = default;\n"
- "};",
+ "};\n",
Style);
verifyFormat("class [[nodiscard]] S {\n"
" S(S&&) = default;\n"
- "};",
+ "};\n",
Style);
verifyFormat("class __attribute((maybeunused)) S {\n"
" S(S&&) = default;\n"
- "};",
+ "};\n",
Style);
verifyFormat("struct S {\n"
" S(S&&) = default;\n"
- "};",
+ "};\n",
Style);
verifyFormat("struct [[nodiscard]] S {\n"
" S(S&&) = default;\n"
- "};",
+ "};\n",
Style);
}
@@ -15731,7 +15731,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
verifyFormat("//\n"
"#define a \\\n"
" if \\\n"
- " 0",
+ " 0\n",
getChromiumStyle(FormatStyle::LK_Cpp));
}
@@ -22538,10 +22538,10 @@ TEST_F(FormatTest, UnderstandsPragmas) {
verifyFormat("#pragma comment(linker, \\\n"
" \"argument\" \\\n"
- " \"argument\"",
+ " \"argument\"\n",
"#pragma comment(linker, \\\n"
" \"argument\" \\\n"
- " \"argument\"",
+ " \"argument\"\n",
getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32));
}
@@ -26160,7 +26160,7 @@ TEST_F(FormatTest, ChromiumDefaultStyle) {
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
verifyFormat("extern \"C\" {\n"
"int foo();\n"
- "}",
+ "}\n",
Style);
}
TEST_F(FormatTest, MicrosoftDefaultStyle) {
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 91577b9a49167..e1700cd24bb86 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -562,7 +562,7 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) {
" b: 'bbbbbbbbbbbbbbbbbb'\n"
"};");
- verifyFormat("f({a: 1, b: 2, c: 3});",
+ verifyFormat("f({a: 1, b: 2, c: 3});\n",
getChromiumStyle(FormatStyle::LK_JavaScript));
verifyFormat("f({'a': [{}]});");
}
diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index e01c1d6d7e684..19782105140fd 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -60,7 +60,7 @@ TEST_F(FormatTestJava, Chromium) {
" for (;;) f();\n"
" if (true) f();\n"
" }\n"
- "}",
+ "}\n",
getChromiumStyle(FormatStyle::LK_Java));
}
@@ -280,7 +280,7 @@ TEST_F(FormatTestJava, Annotations) {
verifyFormat("@Partial @Mock DataLoader loader;");
verifyFormat("@Partial\n"
"@Mock\n"
- "DataLoader loader;",
+ "DataLoader loader;\n",
getChromiumStyle(FormatStyle::LK_Java));
verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
"public static int iiiiiiiiiiiiiiiiiiiiiiii;");
diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index f7f73db62045c..22416bceb2265 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/unittests/Format/FormatTestObjC.cpp
@@ -951,7 +951,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
" styleMask:NSBorderlessWindowMask\n"
" backing:NSBackingStoreBuffered\n"
" defer:NO]);\n"
- "}");
+ "}\n");
// Respect continuation indent and colon alignment (e.g. when object name is
// short, and first selector is the longest one)
|
owenca
reviewed
May 22, 2025
This addresses an old TODO in the Chromium codebase to upstream a change to the Chromium format style. https://source.chromium.org/chromium/chromium/src/+/main:.clang-format;l=10;drc=b9d8d6aeeef9feacf6eb4838cdccca4c2da2a0eb Bug: crbug.com/40247920
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This addresses an old TODO in the Chromium codebase to upstream a change to the Chromium format style.
https://source.chromium.org/chromium/chromium/src/+/main:.clang-format;l=10;drc=b9d8d6aeeef9feacf6eb4838cdccca4c2da2a0eb
Bug: crbug.com/40247920